<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" inline v-show="showSearch" label-width="80px">
      <el-form-item label="合同日期" prop="contractDate">
        <el-date-picker clearable v-model="queryParams.contractDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择合同日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="合同类型" prop="shoppingCarEnums">
        <el-select v-model="queryParams.shoppingCarEnums">
          <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:manager:add']"
        >
          新增
        </el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:manager:remove']"
        >
          删除
        </el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="dataList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="合同编号" align="center" prop="id" />
      <el-table-column label="订单编号" align="center" prop="orderItemSn" />
      <el-table-column label="合同日期" align="center" prop="contractDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.contractDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同金额" align="center" prop="updatePrice" />
      <el-table-column label="合同经手人" align="center" prop="handledBy" />
      <el-table-column label="合同类型" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.shoppingCarEnums | ctCostType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同状态" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.contractStatus | ctContractStatus }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="downloadContract(scope.row)">下载模板合同</el-button>
          <el-button size="mini" type="text" @click="downloadContractStamp(scope.row)" v-if="scope.row.attachment">下载盖章合同</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, 'update')"
            v-hasPermi="['system:manager:edit']" v-if="scope.row.contractStatus != 3">
            修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleUpdate(scope.row, 'detials')"
            v-hasPermi="['system:manager:edit']">
            详情
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:manager:remove']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.params.pageNum"
      :limit.sync="queryParams.params.pageSize" @pagination="getList" />

    <!-- 添加或修改合同管理对话框 -->
    <el-dialog :title="form.id ? '合同详情' : '新增合同'" :visible.sync="open" width="50%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-form-item label="订单编号" prop="id">
          <el-input v-model="form.id" placeholder="请输入合同经手人" :disabled="form.contractStatus == 2" />
        </el-form-item>
        <el-form-item label="合同日期" prop="contractDate">
          <el-date-picker clearable v-model="form.contractDate" type="date" style="width: 100%" value-format="yyyy-MM-dd"
            placeholder="请选择合同日期" :disabled="type != 'update' || form.contractStatus != 2">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同金额" prop="contractAmount">
          <el-input v-model="form.contractAmount" placeholder="请输入合同金额" :disabled="form.contractStatus == 2">
            <el-button slot="append">元</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="合同经手人" prop="handledBy">
          <el-input v-model="form.handledBy" placeholder="请输入合同经手人" :disabled="form.contractStatus == 2" />
        </el-form-item>
        <el-form-item label="用户地址" prop="userAddress">
          <el-input v-model="form.userAddress" placeholder="请输入用户地址" :disabled="form.contractStatus == 2" />
        </el-form-item>
        <el-form-item label="用户联系电话" prop="userContactPhone">
          <el-input v-model="form.userContactPhone" placeholder="请输入用户联系电话" :disabled="form.contractStatus == 2" />
        </el-form-item>
        <el-form-item label="用户姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户姓名" :disabled="form.contractStatus == 2" />
        </el-form-item>
        <el-form-item label="用户开户银行" prop="userBankOfDeposit">
          <el-input v-model="form.userBankOfDeposit" placeholder="请输入用户开户银行" :disabled="form.contractStatus == 2" />
        </el-form-item>
        <el-form-item label="用户银行账号" prop="userAccountNumber">
          <el-input v-model="form.userAccountNumber" placeholder="请输入用户银行账号" :disabled="form.contractStatus == 2" />
        </el-form-item>
        <el-form-item label="纳税人识别编号" prop="userTaxpayerNumber">
          <el-input v-model="form.userTaxpayerNumber" placeholder="请输入纳税人识别编号" :disabled="form.contractStatus == 2" />
        </el-form-item>
        <el-form-item label="公司地址" prop="companyAddress">
          <el-input v-model="form.companyAddress" placeholder="请输入公司地址" :disabled="form.contractStatus == 2" />
        </el-form-item>
        <el-form-item label="公司联系电话" prop="companyContactPhone">
          <el-input v-model="form.companyContactPhone" placeholder="请输入公司联系电话" :disabled="form.contractStatus == 2" />
        </el-form-item>
        <el-form-item label="公司联系人姓名" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司联系人姓名" :disabled="form.contractStatus == 2" />
        </el-form-item>
        <el-form-item label="公司银行账号" prop="companyAccountNumber">
          <el-input v-model="form.companyAccountNumber" placeholder="请输入公司银行账号" :disabled="form.contractStatus == 2" />
        </el-form-item>
        <!-- <el-form-item label="是否是草稿" prop="draft">
          <el-radio-group v-model="form.draft">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否作废" prop="cancel">
          <el-radio-group v-model="form.cancel">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item label="附件地址" prop="attachment">
          <File-upload v-model="form.attachment" :fileType="['doc', 'xls', 'ppt', 'txt', 'pdf', 'png', 'jpg', 'jpeg']"
            label="点击上传盖章合同" :isShowTip="false" :limit="1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="type === 'update'">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listManager,
  getManager,
  delManager,
  addManager,
  updateManager,
  contractDownload,
  getContractDownloadStamp
} from "@/api/system/manager";

export default {
  name: "Manager",
  data() {
    return {
      loading: false, // 遮罩层
      ids: [], // 选中数组
      single: true, // 非单个禁用
      multiple: true, // 非多个禁用
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      dataList: [], // 合同管理表格数据
      open: false, // 是否显示弹出层
      // 查询参数
      queryParams: {
        contractDate: null,
        shoppingCarEnums: null,
        params: {
          pageNum: 1,
          pageSize: 10,
        },
      },
      form: {}, // 表单参数
      rules: {
        contractDate: [{ required: true, message: "用户昵称不能为空", trigger: "blur" }]
      }, // 表单校验
      //合同类型列表
      typeList: [
        { value: "CLOUD_SERVER", label: "云服务器" },
        { value: "CLOUD_MARKET", label: "云市场" },
        { value: "GOV_CLOUD", label: "政务云" },
        { value: "CLOUD_SERVER_RENEW", label: "云服务器续费" },
        { value: "CLOUD_SERVER_CHANGE", label: "云服务器变更" },
      ],
      type: 'update',
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /**下载盖章合同 */
    downloadContractStamp(row) {
      this.$confirm(`确定要下载该盖章合同吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const fileName = row.attachment.replace('/profile', '');
        getContractDownloadStamp({ delete: false, fileName }).then(res => {
          let fileName = res.headers["content-disposition"]?.split(";")[1];
          let name = decodeURIComponent(fileName).split("=")[1];
          let alink = document.createElement("a");
          alink.href = window.URL.createObjectURL(new Blob([res.data]));
          alink.download = name;
          alink.click();
          alink.remove();
        })
      }).catch(() => {
      });
    },
    /**下载模板合同 */
    downloadContract(row) {
      this.$confirm(`确定要下载该合同吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        contractDownload([row.id]).then(res => {
          let fileName = res.headers["content-disposition"]?.split(";")[1];
          let name = decodeURIComponent(fileName).split("=")[1];
          console.log(name);
          let alink = document.createElement("a");
          alink.href = window.URL.createObjectURL(new Blob([res.data]));
          alink.download = name;
          alink.click();
          alink.remove();
        })
      }).catch(() => {
      });
    },
    /** 查询合同管理列表 */
    getList() {
      this.loading = true;
      listManager(this.queryParams)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        contractDate: null,
        contractAmount: null,
        handledBy: null,
        contractStatus: null,
        userAddress: null,
        userContactPhone: null,
        userName: null,
        userBankOfDeposit: null,
        userAccountNumber: null,
        userTaxpayerNumber: null,
        companyAddress: null,
        companyContactPhone: null,
        companyName: null,
        companyAccountNumber: null,
        draft: false,
        cancel: false,
        attachment: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.params.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      this.reset();
      const id = row.id || this.ids;
      this.type = type;
      getManager(id).then((res) => {
        this.form = res.data;
        this.open = true;
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            updateManager(this.form).then((res) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addManager(this.form).then((res) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row ? [row.id] : this.ids;
      console.log(ids,'删除');
      this.$modal
        .confirm('确认删除合同管理编号为"' + ids + '"的数据项？')
        .then(() => {
          delManager(ids).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          });
        })
        .catch(() => { });
    },
  },
};
</script>
